//package com.ev;
//
//import com.csvreader.CsvReader;
//import com.vividsolutions.jts.geom.Coordinate;
//import com.vividsolutions.jts.geom.Geometry;
//import com.vividsolutions.jts.geom.GeometryFactory;
//import com.vividsolutions.jts.geom.Point;
//import org.geotools.data.DefaultTransaction;
//import org.geotools.data.Transaction;
//import org.geotools.data.collection.ListFeatureCollection;
//import org.geotools.data.shapefile.ShapefileDataStore;
//import org.geotools.data.shapefile.ShapefileDataStoreFactory;
//import org.geotools.data.simple.SimpleFeatureCollection;
//import org.geotools.data.simple.SimpleFeatureSource;
//import org.geotools.data.simple.SimpleFeatureStore;
//import org.geotools.feature.simple.SimpleFeatureBuilder;
//import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
//import org.geotools.geometry.jts.JTSFactoryFinder;
//import org.geotools.referencing.crs.DefaultGeographicCRS;
//import org.geotools.swing.data.JFileDataStoreChooser;
//import org.opengis.feature.simple.SimpleFeature;
//import org.opengis.feature.simple.SimpleFeatureType;
//
//import javax.swing.*;
//import java.io.File;
//import java.io.IOException;
//import java.io.Serializable;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Description:
// * @Author: GuoShouFu
// * @Date: 2022/6/27
// **/
//public class GenericShp {
//
//    public void genericShp() {
//
//    }
//
//    private static SimpleFeatureType createFeatureType(Class<? extends Geometry> geometryClazz) {
//        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
//        builder.setName("Location");
//        // <- Coordinate reference system
//        builder.setCRS(DefaultGeographicCRS.WGS84);
//
//        // add attributes in order
//        builder.add("the_geom", geometryClazz);
//        // <- 15 chars width for name field
//        builder.length(15).add("Name", String.class);
//        builder.add("number", Integer.class);
//        // build the type
//        return builder.buildFeatureType();
//    }
//
//    /**
//     * csv文件的记录封装到SimpleFeature集合中
//     *
//     * @param type
//     * @param file
//     * @return
//     * @throws IOException
//     */
//    private static List<SimpleFeature> readCsv2Feature(SimpleFeatureType type, File file) throws IOException {
//        //A list to collect features as we create them.
//        List<SimpleFeature> features = new ArrayList<>();
//        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
//
//        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
//
//        CsvReader csvReader = new CsvReader(file.getAbsolutePath(), ',', StandardCharsets.UTF_8);
//        //获取表头
//        csvReader.readHeaders();
//        while (csvReader.readRecord()) {
//            double latitude = Double.parseDouble(csvReader.get(0));
//            double longitude = Double.parseDouble(csvReader.get(1));
//            String name = csvReader.get(2).trim();
//            int number = Integer.parseInt(csvReader.get(3).trim());
//
//            Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
//            featureBuilder.add(point);
//            featureBuilder.add(name);
//            featureBuilder.add(number);
//            SimpleFeature feature = featureBuilder.buildFeature(null);
//
//            features.add(feature);
//        }
//        return features;
//    }
//
//    /**
//     * 在与csv文件同级目录下创建shp文件
//     *
//     * @param csvFile
//     * @return
//     */
//    private static File getNewShapeFile(File csvFile) {
//        String path = csvFile.getAbsolutePath();
//        String newPath = path.substring(0, path.length() - 4) + ".shp";
//
//        JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
//        chooser.setDialogTitle("Save shapefile");
//        chooser.setSelectedFile(new File(newPath));
//
//        int returnVal = chooser.showSaveDialog(null);
//
//        if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
//            // the user cancelled the dialog
//            System.exit(0);
//        }
//        File newFile = chooser.getSelectedFile();
//        if (newFile.equals(csvFile)) {
//            System.out.println("Error: cannot replace " + csvFile);
//            System.exit(0);
//        }
//        return newFile;
//    }
//
//    public static void main(String[] args) throws Exception {
//        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//
//        File file = JFileDataStoreChooser.showOpenFile("csv", null);
//        if (file == null) {
//            return;
//        }
//
//        //Create a FeatureType
//        SimpleFeatureType type = createFeatureType(Point.class);
//
//        //将csv转换为feature
//        List<SimpleFeature> features = readCsv2Feature(type, file);
//
//
//        //Get an output file name and create the new shapefile
//        File newFile = getNewShapeFile(file);
//
//        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
//
//        Map<String, Serializable> params = new HashMap<>(2);
//        params.put("url", newFile.toURI().toURL());
//        params.put("create spatial index", Boolean.TRUE);
//
//        ShapefileDataStore newDataStore =
//                (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
//
//        //TYPE is used as a template to describe the file contents
//        newDataStore.createSchema(type);
//
//        //Write the features to the shapefile
//        Transaction transaction = new DefaultTransaction("create");
//
//        String typeName = newDataStore.getTypeNames()[0];
//        SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
//
//        if (featureSource instanceof SimpleFeatureStore) {
//            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
//            SimpleFeatureCollection collection = new ListFeatureCollection(type, features);
//            featureStore.setTransaction(transaction);
//            try {
//                featureStore.addFeatures(collection);
//                transaction.commit();
//            } catch (Exception problem) {
//                problem.printStackTrace();
//                transaction.rollback();
//            } finally {
//                transaction.close();
//            }
//            System.exit(0);
//        } else {
//            System.out.println(typeName + " does not support read/write access");
//            System.exit(1);
//        }
//    }
//
//
//
//}
